﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Web;
using Newtonsoft.Json;

namespace ifunction.BaiduBCS.SDK
{
    /// <summary>
    /// Class BaiduBCSClient.
    /// </summary>
    partial class BaiduBCSClient
    {
        /// <summary>
        /// Creates the bucket.
        /// According to bucket naming rule by Baidu, all bucket names would be changed to lower case inside client for any bucket operation.
        /// If the bucket is already existed, you would get http 403 error.
        /// <example>
        /// The following code would show you how to create, get and delete <c>bucket</c> in <c>BaiduBCSClient</c>
        /// <code>
        /// <![CDATA[
        /// // You need to assign your bucket in your code
        /// string bucket = "TestBucket";
        /// // You need to assign your app key and your secret key in your code
        /// var client = new BaiduBCSClient("your app key", "your secret key");
        ///
        /// // According to bucket naming rule by Baidu, all bucket names would be changed to lower case inside client for any bucket operation.
        /// client.CreateBucket(bucket);
        ///
        /// // Get bucket list and output summary via Console.
        /// var bucketList = client.GetBucketList();
        ///
        /// foreach (var one in bucketList)
        /// {
        ///     Console.WriteLine(string.Format("{Bucket: {0}, TotalCapacity: {1}.", one.Name, one.TotalCapacity));
        /// }
        ///
        /// // Delete bucket.
        /// client.DeleteBucket(bucket);
        /// ]]>
        /// </code>
        /// </example>
        /// </summary>
        /// <param name="name">The name.
        /// According to bucket naming rule by Baidu, all bucket names would be changed to lower case inside client for any bucket operation.
        /// </param>
        /// <exception cref="System.InvalidOperationException">CreateBucket</exception>
        public void CreateBucket(string name)
        {
            const string httpVerb = "PUT";

            try
            {
                name.CheckNullOrEmptyString("name");

                string featureUrl = name = name.ToLowerInvariant();

                var httpRequest = this.CreateHttpRequest(featureUrl, httpVerb, name.ToLowerInvariant(), null);
                HttpStatusCode statusCode;
                var responseText = httpRequest.ReadResponseAsText(null, out statusCode);

                TryThrowExceptionByStatusCode(statusCode, responseText);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("CreateBucket", ex);
            }
        }

        /// <summary>
        /// Gets the bucket list.
        /// <example>
        /// The following code would show you how to create, get and delete <c>bucket</c> in <c>BaiduBCSClient</c>
        /// <code>
        /// <![CDATA[
        /// // You need to assign your bucket in your code
        /// string bucket = "TestBucket";
        /// // You need to assign your app key and your secret key in your code
        /// var client = new BaiduBCSClient("your app key", "your secret key");
        ///
        /// // According to bucket naming rule by Baidu, all bucket names would be changed to lower case inside client for any bucket operation.
        /// client.CreateBucket(bucket);
        ///
        /// // Get bucket list and output summary via Console.
        /// var bucketList = client.GetBucketList();
        ///
        /// foreach (var one in bucketList)
        /// {
        ///     Console.WriteLine(string.Format("{Bucket: {0}, TotalCapacity: {1}.", one.Name, one.TotalCapacity));
        /// }
        ///
        /// // Delete bucket.
        /// client.DeleteBucket(bucket);
        /// ]]>
        /// </code>
        /// </example>
        /// </summary>
        /// <returns>List of <see cref="Bucket"/> instances.</returns>
        /// <exception cref="System.InvalidOperationException">GetBucketList</exception>
        public List<Bucket> GetBucketList()
        {
            const string featureUrl = "";
            const string httpVerb = "GET";

            try
            {
                var httpRequest = this.CreateHttpRequest(featureUrl, httpVerb, string.Empty, null);
                HttpStatusCode statusCode;
                var responseText = httpRequest.ReadResponseAsText(Encoding.UTF8, out statusCode);

                TryThrowExceptionByStatusCode(statusCode, responseText);

                return string.IsNullOrWhiteSpace(responseText) ? new List<Bucket>() :
                    JsonConvert.DeserializeObject<List<Bucket>>(responseText);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("GetBucketList", ex);
            }
        }

        /// <summary>
        /// Deletes the bucket.
        /// According to bucket naming rule by Baidu, all bucket names would be changed to lower case inside client for any bucket operation.
        /// If the bucket is not existed, you would get http 403 error.
        /// <example>
        /// The following code would show you how to create, get and delete <c>bucket</c> in <c>BaiduBCSClient</c>
        /// <code>
        /// <![CDATA[
        /// // You need to assign your bucket in your code
        /// string bucket = "TestBucket";
        /// // You need to assign your app key and your secret key in your code
        /// var client = new BaiduBCSClient("your app key", "your secret key");
        ///
        /// // According to bucket naming rule by Baidu, all bucket names would be changed to lower case inside client for any bucket operation.
        /// client.CreateBucket(bucket);
        ///
        /// // Get bucket list and output summary via Console.
        /// var bucketList = client.GetBucketList();
        ///
        /// foreach (var one in bucketList)
        /// {
        ///     Console.WriteLine(string.Format("{Bucket: {0}, TotalCapacity: {1}.", one.Name, one.TotalCapacity));
        /// }
        ///
        /// // Delete bucket.
        /// client.DeleteBucket(bucket);
        /// ]]>
        /// </code>
        /// </example>
        /// </summary>
        /// <param name="name">The name.
        /// According to bucket naming rule by Baidu, all bucket names would be changed to lower case inside client for any bucket operation.
        /// </param>
        /// <exception cref="System.InvalidOperationException">DeleteBucket</exception>
        public void DeleteBucket(string name)
        {
            const string httpVerb = "DELETE";

            try
            {
                name.CheckNullOrEmptyString("name");
                string featureUrl = name = name.ToLowerInvariant();

                var httpRequest = this.CreateHttpRequest(featureUrl, httpVerb, name.ToLowerInvariant(), null);
                HttpStatusCode statusCode;
                var responseText = httpRequest.ReadResponseAsText(null, out statusCode);

                TryThrowExceptionByStatusCode(statusCode, responseText);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("DeleteBucket", ex);
            }
        }
    }
}
